﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using EntreeServer.Module;
using EntreeServer.Module.Controls;
using EntreeServer.Module.ActionTray;
using Microsoft.SharePoint;

namespace ME.SDK.TestApplications
{
    [Guid("a4d43f1b-6ac1-41dc-9f7e-e1e2a13c685a")]
    public class CalendarControlSample : EntreeApplication
    {

        public override string PluginPath
        {
            get
            {
                return "/_layouts/images/ME.SDK.TestApplications/CalendarControlSample";
            }
        }

        public override void QueryList(EntreeScopeObject EntreeScope, EntreeListObject output)
        {

            string screen = "cal";
            if (EntreeScope.Properties.ContainsKey("screen")) screen = EntreeScope.Properties["screen"];

            if (screen == "cal")
            {
                //Render the initial Calendar---------

                var myGroup = output.AddListGroup();
                myGroup.ListHeader = "Calendar";
                myGroup.ListGroupStyle = EntreeListGroupStyles.calendar;

                //add a calendar control to the list
                var calendar = myGroup.AddListItem<EntreeListItemCalendar>();
                calendar.Value = DateTime.Now;
                calendar.MinYear = DateTime.Now.Year - 5;
                calendar.MaxYear = DateTime.Now.Year + 5;

                SPWeb web = SPContext.Current.Web;

                var list = web.Lists["Calendar"];

                var view = list.DefaultView;
                var q = new SPQuery
                {
                    Query = view.Query,
                    CalendarDate = calendar.Value,
                    ViewXml = view.SchemaXml,
                    RecurrenceOrderBy = view.RecurrenceRowset,
                    ExpandRecurrence = view.RecurrenceRowset,
                    ViewFields = view.ViewFields.Cast<string>().Aggregate((sum, value) => sum += "<FieldRef Name=\"{0}\" />".FormatString(value))
                };

                var lSet = view.ParentList.GetItems(q);

                var fields = view.ViewFields.Cast<string>().Take(2).ToArray();
                foreach (SPListItem item in lSet)
                {
                    var title = item.Title.ToString();
                    var caption = item[fields[0]].ToString();
                    var startDate = item[fields[0]] == null ? DateTime.MinValue : (DateTime)item[fields[0]];
                    var endDate = item[fields[1]] == null ? DateTime.MinValue : (DateTime)item[fields[1]];
                    var calItem = new EntreeCalendarItem()
                    {
                        Title = title,
                        Caption = caption,
                        EndDate = endDate,
                        StartDate = startDate
                    };
                    var clickObj = new EntreeClickActionObject(this);
                    clickObj["screen"] = "item";
                    clickObj["itemTitle"] = title;
                    clickObj["itemId"] = item.ID.ToString();
                    calItem.ClickAction = clickObj;
                    calendar.Items.Add(calItem);
                }

                //Set scope for loading Actions
                output.ActionScope.ScopeType = ScopeTypes.SharePoint.SPView;
                output.ActionScope.Properties[EntreeServer.Module.ActionTray.ScopeProperties.SharePoint.SPListID] = list.ID.ToString();
                output.ActionScope.Properties[EntreeServer.Module.ActionTray.ScopeProperties.SharePoint.SPViewID] = view.ID.ToString();
            }
            else
            {
                output.ActionScope.ScopeType = "";
                //display the selected item
                var itemgrp = output.AddListGroup("Item View");
                var bb = itemgrp.AddListItem<EntreeListItemBillboard>();
                bb.InnerHtml = "<b>" + EntreeScope.Properties["itemTitle"] + "</b>";
                bb.InnerHtml += "<div>additional content...</div>";

                BackActionItem back = new BackActionItem();
                back.Title = "Back";
                back.ActionId = "BackAction";
                back.ClickAction = new EntreeClickActionObject(this)
                {
                    AnimationStyle = EntreeAnimationStyles.SLIDE_RIGHT
                };
                back.ClickAction["screen"] = "cal";
                output.HomeBar.ActionItems.Items.Add(back);
            }
        }

        public override void ActiveUpdate(EntreeScopeObject scope, EntreeListItemCollection output)
        {
            var calendar = output.AddListItem<EntreeListItemCalendar>();
            calendar.Value = scope.Properties.ContainsKey("Calendar_Year") ? new DateTime(int.Parse(scope.Properties["Calendar_Year"]),int.Parse(scope.Properties["Calendar_Month"]),1) : DateTime.Now;
            calendar.MinYear = DateTime.Now.Year - 5;
            calendar.MaxYear = DateTime.Now.Year + 5;

            SPWeb web = SPContext.Current.Web;

            var list = web.Lists["Calendar"];

            //add items for current month to calendar
            var view = list.DefaultView;
            var q = new SPQuery
            {
                Query = view.Query,
                CalendarDate = calendar.Value,
                ViewXml = view.SchemaXml,
                RecurrenceOrderBy = view.RecurrenceRowset,
                ExpandRecurrence = view.RecurrenceRowset,
                ViewFields = view.ViewFields.Cast<string>().Aggregate((sum, value) => sum += "<FieldRef Name=\"{0}\" />".FormatString(value))
            };

            var lSet = view.ParentList.GetItems(q);

            var fields = view.ViewFields.Cast<string>().Take(2).ToArray();
            foreach (SPListItem item in lSet)
            {
                var title = item.Title.ToString();
                var caption = item[fields[0]].ToString();
                var startDate = item[fields[0]] == null ? DateTime.MinValue : (DateTime)item[fields[0]];
                var endDate = item[fields[1]] == null ? DateTime.MinValue : (DateTime)item[fields[1]];
                var calItem = new EntreeCalendarItem()
                {
                    Title = title,
                    Caption = caption,
                    EndDate = endDate,
                    StartDate = startDate
                };
                var clickObj = new EntreeClickActionObject(this);
                clickObj["screen"] = "item";
                clickObj["itemTitle"] = title;
                clickObj["itemId"] = item.ID.ToString();
                calItem.ClickAction = clickObj;
                calendar.Items.Add(calItem);
            }
        }

        public override string Title
        {
            get { return "CalendarControlSample"; }
        }
    }
}
